package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        List<Setmeal> setmealList = setmealMapper.pageQuery(setmealPageQueryDTO);
        Page<Setmeal> page = (Page<Setmeal>) setmealList;
        List<Setmeal> records = page.getResult();
        long total = page.getTotal();
        return new PageResult(total, records);
    }

    /**
     * 新增套擦
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void saveSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //先对套餐表进行新增
        setmealMapper.saveSetmeal(setmeal);
        //获取当前套餐表的id
        Long setmealId = setmeal.getId();
        //对套餐菜品关系表进行新增
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
        }
        setmealDishMapper.saveSetmealDish(setmealDishes);

    }


    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO querySetmealById(Long id) {
        // 1. 查询套餐基本信息
        Setmeal setmeal = setmealMapper.getSetmealById(id);

        SetmealVO setmealVO=new SetmealVO();
        if(setmeal!=null){
            BeanUtils.copyProperties(setmeal,setmealVO);
        }

        // 2. 查询套餐菜品
        List<SetmealDish> dishes = setmealDishMapper.getDishesBySetmealId(id);
        setmealVO.setSetmealDishes(dishes);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) {
        // 前置校验
        if (setmealDTO.getCategoryId() == null) {
            throw new RuntimeException("套餐ID不能为空");
        }
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //1、先修改setmeal
        setmealMapper.updateSetmeal(setmeal);
        //2、修改套菜关系表
        // 2. 更新套餐菜品关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && !setmealDishes.isEmpty()) {
            // 先删除原有菜品关系
            setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
            setmealDishMapper.saveSetmealDish(setmealDishes);
        }

    }

    /**
     * 套餐起售、停售
     * @param status
     * @param id
     * @return
     */
    @Override
    public void StartOrStop(Integer status, Long id) {
        Setmeal setmeal=new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.updateSetmeal(setmeal);
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @Override
    public void deleteSetmealBatch(List<Long> ids) {
        if(ids.size()==0 && ids==null){
            return;
        }
        setmealMapper.deleteSetmealBatch(ids);
    }
        public List<Setmeal> list(Setmeal setmeal) {
            List<Setmeal> list = setmealMapper.list(setmeal);
            return list;
        }

        /**

         * 根据id查询菜品选项
         * @param id
         * @return
         */
        public List<DishItemVO> getDishItemById(Long id) {
            return setmealMapper.getDishItemBySetmealId(id);
        }
    }

